﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security;
using System.Security.Cryptography;
namespace Twi.COMMON.WCF
{
    public class FileProtector
    {
        private static string key = "TWI"; //默认密钥 

        private static byte[] sKey;
        private static byte[] sIV;

        //加密方法
        public static bool EncryptFile(string filePath, string savePath, string keyStr)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                if (keyStr == "")
                    keyStr = key;
                FileStream fs = File.OpenRead(filePath);
                byte[] inputByteArray = new byte[fs.Length];
                fs.Read(inputByteArray, 0, (int)fs.Length);
                fs.Close();
                byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
                SHA1 ha = new SHA1Managed();
                byte[] hb = ha.ComputeHash(keyByteArray);
                sKey = new byte[8];
                sIV = new byte[8];
                for (int i = 0; i < 8; i++)
                    sKey[i] = hb[i];
                for (int i = 8; i < 16; i++)
                    sIV[i - 8] = hb[i];
                des.Key = sKey;
                des.IV = sIV;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                fs = File.OpenWrite(savePath);
                foreach (byte b in ms.ToArray())
                {
                    fs.WriteByte(b);
                }
                fs.Close();
                cs.Close();
                ms.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }

        //加密方法
        public static byte[] EncryptFile(byte[] inputByteArray, string keyStr = "")
        {
            List<byte> encryptBuffer = new List<byte>();
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                if (keyStr == "")
                    keyStr = key;
                
                byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
                SHA1 ha = new SHA1Managed();
                byte[] hb = ha.ComputeHash(keyByteArray);
                sKey = new byte[8];
                sIV = new byte[8];
                for (int i = 0; i < 8; i++)
                    sKey[i] = hb[i];
                for (int i = 8; i < 16; i++)
                    sIV[i - 8] = hb[i];
                des.Key = sKey;
                des.IV = sIV;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();

                while (true)
                {
                    int ret = ms.ReadByte();
                    if (ret == -1)
                    {
                        break;
                    }
                    else
                    {
                        encryptBuffer.Add((byte)ret);
                    }
                }
                cs.Close();
                ms.Close();
            }
            catch
            {

            }
            return encryptBuffer.ToArray();
        }

        // 解密方法
        public static bool DecryptFile(string filePath, string savePath, string keyStr)
        {
            try
            {
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                if (keyStr == "")
                    keyStr = key;
                FileStream fs = File.OpenRead(filePath);
                byte[] inputByteArray = new byte[fs.Length];
                fs.Read(inputByteArray, 0, (int)fs.Length);
                fs.Close();
                byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
                SHA1 ha = new SHA1Managed();
                byte[] hb = ha.ComputeHash(keyByteArray);
                sKey = new byte[8];
                sIV = new byte[8];
                for (int i = 0; i < 8; i++)
                    sKey[i] = hb[i];
                for (int i = 8; i < 16; i++)
                    sIV[i - 8] = hb[i];
                des.Key = sKey;
                des.IV = sIV;
                MemoryStream ms = new MemoryStream();
                CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                fs = File.OpenWrite(savePath);
                foreach (byte b in ms.ToArray())
                {
                    fs.WriteByte(b);
                }
                fs.Close();
                cs.Close();
                ms.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
